package com.ztesoft.ossup.report.service;

import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ztesoft.ossup.report.web.ReportController;

@Service("baseService")
public class BaseService {
	 Logger _logger = Logger.getLogger(BaseService.class);
	public String doSql(String sql,Map paramMap){
		sql = sql.toUpperCase();
		if (sql != null && !sql.equals("")){
    		Iterator itera = paramMap.entrySet().iterator();    	
        	while(itera.hasNext()){
        		//_logger.info("---------Map数据----------");
        		Map.Entry entry = (Map.Entry) itera.next();
        		String key = (String) entry.getKey();
        		String value =  entry.getValue() ==null?"":entry.getValue().toString();
        		//"<a>"+dataMap[key]+"</a>" 剔除掉a标签
        		value = value.replaceAll("<a>", "").replace("</a>", "");
        		String keyStr = "#"+key+"#";
				_logger.info("key:"+key+",value:"+value);
        		while (sql.indexOf(keyStr) > -1 ){
        			//_logger.info("value值为："+value);
        			if (!value.equals("") && value != null && !value.equals("null")){
        				sql = sql.replaceAll("#"+key+"#", "'"+value+"'");
        			}else {//and ua.area_name = #AERA_NAME# 去掉
        				String subSql = sql;
//        				int endIndex = sql.indexOf(keyStr) + keyStr.length();
//        				sql = sql.substring(0,endIndex);
//        				int andIndex = sql.lastIndexOf("AND");
//        				sql = subSql.substring(0, andIndex) + subSql.substring(endIndex);
        				
        				int endIndex = sql.lastIndexOf(keyStr);
        				sql = sql.substring(0,endIndex);
        				int andIndex = sql.lastIndexOf("AND");
        				String afterSql = subSql.substring(endIndex+1);
        				if (!afterSql.trim().equals("")){
        					int afterAndIndex = afterSql.indexOf("AND");
        					afterSql = afterSql.substring(afterAndIndex);
        				}
        				sql = subSql.substring(0, andIndex) + afterSql;
        			}
        		}
        	}
        }
		sql = doSqlNoParams(sql);//排除一些错误的包涵＃号
		return sql;
	}
	public String doSqlNoParams(String sql){
		sql = sql.toUpperCase();
		int length = sql.length();
		if (sql != null && !sql.equals("")){
			while(sql.lastIndexOf("#") > -1){
				String subSql = sql;
				int endIndex = sql.lastIndexOf("#");
				sql = sql.substring(0,endIndex);
				int andIndex = sql.lastIndexOf("AND");
				String afterSql = subSql.substring(endIndex+1);
				if (!afterSql.trim().equals("")){
					int afterAndIndex = afterSql.indexOf("AND");
					afterSql = afterSql.substring(afterAndIndex);
				}
				sql = subSql.substring(0, andIndex) + afterSql;
			}
        }
		_logger.info("执行的sql为："+sql);
		return sql;
	}
	public String doSqlNoParamsForSqlConfig(String sql){
		sql = sql.toUpperCase();
		if (sql != null && !sql.equals("")){
			while(sql.lastIndexOf("#") > -1){
				String subSql = sql;
				int endIndex = sql.lastIndexOf("#");
				sql = sql.substring(0,endIndex);
				int andIndex = sql.lastIndexOf("AND");
				sql = subSql.substring(0, andIndex) + subSql.substring(endIndex+2);
			}
        }
		_logger.info("执行的sql为："+sql);
		return sql;
	}
	
}

